<template>
  <div>
    <el-table :data="noticeList" border style="width: 100%">
      <el-table-column type="index" label="ID" width="50"></el-table-column>
      <el-table-column prop="title" label="公告标题"></el-table-column>
      <el-table-column prop="created_at" label="时间" width="160"></el-table-column>
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
          <el-button @click="info(scope.row)" size="mini">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="margin-top-20 text-center" v-if="noticeList.length">
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="currentPageChange"
        :background="true"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="noticeInfo.title" :visible.sync="dialogInfoVisible">
      <div v-html="noticeInfo.content"></div>
      <div class="text-center margin-top-30" v-if="noticeInfo.isSignUp">
        <el-button type="success" size="small" @click="signUp()">我要报名</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      noticeList: [],
      page: {
        total: 0,
        currentPage: 1,
        size: 10
      },
      noticeInfo: {
        id: 0,
        title: '',
        content: '',
        isSignUp: 0
      },
      dialogInfoVisible: false
    }
  },
  mounted() {
    this.getNoticeListData();
  },
  methods: {
    getNoticeListData() {
      this.axios.get('/doctor/notices/doctor/list', {
        params: {
          pageSize: this.page.size,
          currentPage: this.page.currentPage
        }
      }).then((response) => {
        if(response.data.code == 200){
          this.noticeList = response.data.data.data;
          this.page.total = response.data.data.total;
        }else{
          this.$message({
            message: '数据加载失败!',
            type: 'error'
          });
        }
      });
    },
    pageSizeChange(val) {
      this.page.currentPage = 1;
      this.page.size = val;
      this.getNoticeListData();
    },
    currentPageChange(val) {
      this.page.currentPage = val;
      this.getNoticeListData();
    },
    info(row) {
      this.axios.get('/doctor/notices/'+row.id).then((response) => {
        if(response.data.code == 200){
          this.noticeInfo.id = row.id;
          this.noticeInfo.title = response.data.data.title;
          this.noticeInfo.content = response.data.data.content;
          this.noticeInfo.isSignUp = response.data.data.isSignUp;
          this.dialogInfoVisible = true;
        }
      });
    },
    signUp() {
      this.axios.put('/doctor/noticeUsers/add', {
        noticeId: this.noticeInfo.id,
        userId: this.$store.state.doctorInfo.id,
        role: 'doctor'
      }).then((response) => {
        if(response.data.code == 200){
          this.$message({
            message: response.data.msg,
            type: 'success'
          });
        }else{
          this.$message({
            message: response.data.msg,
            type: 'error'
          });
        }
      });
    }
  }
}
</script>
